package com.test.demos.config;

import cn.dev33.satoken.stp.StpInterface;
import com.mybatisflex.core.query.QueryWrapper;
import com.test.demos.mapper.PermissionMapper;
import com.test.demos.mapper.RoleMapper;
import com.test.demos.mapper.RolePermissionMapper;
import com.test.demos.mapper.UserRoleMapper;
import com.test.demos.mapper.UsersMapper;
import com.test.demos.pojo.entity.Permission;
import com.test.demos.pojo.entity.Role;
import com.test.demos.pojo.entity.RolePermission;
import com.test.demos.pojo.entity.UserRole;
import com.test.demos.pojo.entity.Users;
import com.test.demos.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限加载接口实现类
 */
@Component    // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private RoleMapper roleMapper;
    /**
     * 返回一个账号所拥有的权限码集合 
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 初始化权限码列表
        List<String> permissionList = new ArrayList<>();

        // 1. 根据用户 ID 查询用户角色关联信息
        QueryWrapper userRoleQueryWrapper = QueryWrapper.create()
                .eq(UserRole::getUserId, loginId);
        List<UserRole> userRoles = userRoleMapper.selectListByQuery(userRoleQueryWrapper);

        // 2. 遍历用户角色关联信息，获取角色 ID
        for (UserRole userRole : userRoles) {
            Integer roleId = userRole.getRoleId();

            // 3. 根据角色 ID 查询角色权限关联信息
            QueryWrapper rolePermissionQueryWrapper = QueryWrapper.create()
                    .eq(RolePermission::getRoleidId, roleId);
            List<RolePermission> rolePermissions = rolePermissionMapper.selectListByQuery(rolePermissionQueryWrapper);

            // 4. 遍历角色权限关联信息，获取权限 ID
            for (RolePermission rolePermission : rolePermissions) {
                Integer permissionId = rolePermission.getPermissionId();

                // 5. 根据权限 ID 查询权限信息
                Permission permission = permissionMapper.selectOneById(permissionId);
                if (permission != null) {
                    // 6. 将权限码添加到列表中
                    permissionList.add(permission.getPermissionCode());
                }
            }
        }
        return permissionList;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roleList = new ArrayList<>();

        // 根据 loginId 查询用户角色关联信息
        QueryWrapper userRoleQueryWrapper = QueryWrapper.create()
                .eq(UserRole::getUserId, loginId);
        List<UserRole> userRoles = userRoleMapper.selectListByQuery(userRoleQueryWrapper);

        // 遍历用户角色关联信息，获取角色标识
        for (UserRole userRole : userRoles) {
            Integer roleId = userRole.getRoleId();
            Role role = roleMapper.selectOneById(roleId);
            if (role != null) {
                // 假设使用 roleCode 作为角色标识
                roleList.add(role.getRoleCode());
            }
        }
        return roleList;
    }
}
